package com.whfc.emp.factory;

import com.whfc.emp.dao.AppFaceGateConfigMapper;
import com.whfc.emp.dao.AppFaceGateMapper;
import com.whfc.emp.entity.AppFaceGate;
import com.whfc.emp.entity.AppFaceGateConfig;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.manager.FaceGateManager;
import com.whfc.emp.manager.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author sun_guodong
 * @description
 * @date 2020-06-20
 */
@Component
public class AppFaceGateServiceFactory {

    /**
     * 宇泛沃土平台
     */
    @Resource(name = "wotuFaceGateManagerImpl")
    private WotuFaceGateManagerImpl wotuFaceGateManagerImpl;

    /**
     * 宇泛沃平台
     */
    @Resource(name = "woFaceGateManagerImpl")
    private WoFaceGateManagerImpl woFaceGateManager;

    /**
     * 小麦科技平台
     */
    @Resource(name = "wheatSunshineFaceGateManagerImpl")
    private WheatSunshineFaceGateManagerImpl wheatSunshineFaceGateManagerImpl;

    /**
     * 其他离线平台
     */
    @Resource(name = "faceGateManagerImpl")
    private FaceGateManagerImpl faceGateManagerImpl;

    /**
     * 海清视讯MQTT
     */
    @Resource(name = "mqttHqsxFaceGateManagerImpl")
    private MqttHqsxFaceGateManagerImpl mqttHqsxFaceGateManager;

    /**
     * 海清视讯MQTT 住建局版本
     */
    @Resource(name = "mqttHqsxJjzFaceGateManagerImpl")
    private MqttHqsxJjzFaceGateManagerImpl mqttHqsxJjzFaceGateManager;

    @Autowired
    private AppFaceGateConfigMapper appFaceGateConfigMapper;

    @Autowired
    private AppFaceGateMapper faceGateMapper;

    /**
     * 根据闸机ID查询闸机服务
     *
     * @param faceGateId 闸机ID
     * @return
     */
    public FaceGateManager getFaceGateService(Integer faceGateId) {
        AppFaceGate faceGate = faceGateMapper.selectByPrimaryKey(faceGateId);
        Integer deptId = faceGate.getDeptId();
        String platform = faceGate.getPlatform();
        return getFaceGateService(deptId, platform);
    }

    /**
     * 根据闸机deviceKey查询闸机服务
     *
     * @param deviceKey
     * @return
     */
    public FaceGateManager getFaceGateService(String deviceKey) {
        AppFaceGate faceGate = faceGateMapper.selectByDeviceKey(deviceKey);
        Integer deptId = faceGate.getDeptId();
        String platform = faceGate.getPlatform();
        return getFaceGateService(deptId, platform);
    }

    /**
     * 根据项目ID和平台查询闸机服务器
     *
     * @param deptId
     * @param platform
     * @return
     */
    public FaceGateManager getFaceGateService(Integer deptId, String platform) {
        AppFaceGateConfig appFaceGateConfig = appFaceGateConfigMapper.selectByDeptIdAndPlatform(deptId, platform);
        FaceGateType faceGateType = FaceGateType.parseCode(platform);
        FaceGateManager faceGateManager = getFaceGateManager(faceGateType.getValue());
        faceGateManager.info(appFaceGateConfig);
        return faceGateManager;
    }

    /**
     * 根据配置ID查询闸机服务
     *
     * @param configId
     * @return
     */
    public FaceGateManager getFaceGateServiceById(Integer configId) {
        AppFaceGateConfig appFaceGateConfig = appFaceGateConfigMapper.selectByPrimaryKey(configId);
        String platform = appFaceGateConfig.getPlatform();
        FaceGateType faceGateType = FaceGateType.parseCode(platform);
        FaceGateManager faceGateManager = getFaceGateManager(faceGateType.getValue());
        faceGateManager.info(appFaceGateConfig);
        return faceGateManager;
    }

    private FaceGateManager getFaceGateManager(Integer value) {
        switch (value) {
            case 1:
                return wotuFaceGateManagerImpl;
            case 2:
                return wheatSunshineFaceGateManagerImpl;
            case 3:
                return woFaceGateManager;
            case 9:
                return mqttHqsxJjzFaceGateManager;
            case 10:
                return mqttHqsxFaceGateManager;
            default:
                return faceGateManagerImpl;
        }
    }
}
